<?


class HtmlTable{
	var $head = array();
	var $content = array();
	var $tableCSS = "inline";
	var $trCSS = "";
	var $tdCSS = "";
	var $thCSS = "";
	function setHead($head,$hasHead = TRUE){

		$this->head = $head;
	}
	function setContent($content){
		$this->content = $content;
	}

	function addContent($row){
		array_push($this->content, $row);
	}


	function getTable(){
		$tCss = "class='".$this->tableCSS."'";
		$trCss = "class='".$this->trCSS."'";
		$tdCss = "class='".$this->tdCSS."'";
		$thCss = "class='".$this->thCSS."'";
		if($this->tableCSS == "") $tCss = "";
		if($this->trCSS == "") $trCss = "";
		if($this->tdCSS == "") $tdCss = "";
		if($this->thCSS == "") $thCss = "";

		$str = "<table $tCss>";
		if(count($this->head) > 0){
			$str .="<thead><tr $trCss>";
			foreach ($this->head as $i => $value) {
				$str .= "<th $thCss>$i</th>";
			}
			$str .= "</tr></thead>";
		}
		$str .="<tbody>";
		for($i = 0; $i < count($this->content);$i++){
			$str .= "<tr $trCss>";
			foreach ($this->content[$i] as $k => $value) {
				$str .= "<td $tdCss>".utf8_encode($value)."</td>";
			}
			$str .= "</tr>";
		}

		return $str . "</tbody></table>";
	}
}

function send_email($email,$title,$msg){
	$from = "Sommeruni";
	$fromemail = "sommerun@zedat.fu-berlin.de";
	mail($email, $title, $msg, "From: $from <$fromemail>");
}

function setFormError($msg){
	return "<b style='color:red'>$msg</b>";
}
function setFormSuccess($msg){
	return "<b style='color:green'>$msg</b>";
}


function createFormData($data,$css = "longedit"){
	$array = array();
	$hiddenfields = "";
	for($i =0;$i < count($data);$i++){
		$tmp = array();

		$tmp['name'] = $data[$i][0];
		switch($data[$i][1]){
			case "hidden":
				$hiddenfields .= "<input class=\"$css\" type=\"hidden\" name=\"".$data[$i][2]."\" value=\"".$data[$i][3]."\" />";
				break;
			case "text":
				$tmp['data'] = "<input class=\"$css\" type=\"text\" name=\"".$data[$i][2]."\" value=\"".$data[$i][3]."\" />";
				break;
			case "nonEditable":
				$show = $data[$i][3];
				if($data[$i][3] == "") $show = "&nbsp;";
				$tmp['data'] = "<input class=\"$css\" type=\"hidden\" name=\"".$data[$i][2]."\" value=\"".$data[$i][3]."\" readonly=\"readonly\" />".$data[$i][3];
				break;
			case "select":
				$tmp['data'] = "<select class=\"$css\" name=\"".$data[$i][2]."\">";
				foreach($data[$i][4] as $k => $v){
					if($k == $data[$i][3])
					$tmp['data'].= "<option value=\"$k\" selected>$v</option>";
					else
					$tmp['data'].= "<option value=\"$k\">$v</option>";
				}
				$tmp['data'].= "</select>";
				break;
			case "area":
				$tmp['data'] = "<textarea class=\"$css\" name=\"".$data[$i][2]."\">".$data[$i][3]."</textarea>";
				break;
			case "date":

				static $idcounter = 0;
				$idcounter++;

				//yyyy-dd-mm
				if($data[$i][3] == ""){
						
					$data[$i][3] = date("Y-m-d");
				}
				$year = substr($data[$i][3], 0,4);
				$day = substr($data[$i][3], 5,2);
				$month = substr($data[$i][3], 8,2);
				$datepickid = "datepickerid".$idcounter;
				$tmp['data'] = "<input type='text' class=\"$css\" name='".$data[$i][2]."' id='".$datepickid."' value='".$data[$i][3]."' />";
				$tmp['data'].="<script type=\"text/javascript\">new JsDatePick({useMode:2,target:\"".$datepickid."\",dateFormat:\"%Y-%m-%d\",selectedDate:{day:$day,month:$month,year:$year},});</script>";
				/*
				 selectedDate:{
				 day:5,
				 month:9,
				 year:2006
				 },
				 yearsRange:[1978,2020],
				 limitToToday:false,
				 cellColorScheme:\"beige\",
				 dateFormat:\"%m-%d-%Y\",
				 imgPath:\"datepicker/\",
				 weekStartDay:1
				 */

				break;
			case "submit":
				$tmp['data'] = "<input class=\"longsubmit\" type=\"submit\" name=\"".$data[$i][2]."\" value=\"".$data[$i][3]."\" />";
				break;
			default:
				trigger_error("Unknown type: ".$data[$i][1]);
		}
		if(isset($tmp['data'])){
			array_push($array,$tmp);
		}
	}
	$array[count($array)-1]['data'] = $array[count($array)-1]['data'].$hiddenfields; 

	return $array;
}
/**
 * Create form row from parameters
 * @param unknown_type $target
 * @param unknown_type $data data array(title,type,name,value,other)
 *
 */
function createFormRow($target,$data,$header = false){
	$out = "";

	for($i=0; $i < count($data);$i++){
	$table = new HtmlTable();
	$table->tableCSS = "formrowtable";
	$formstart = "<form action='$target' method='POST' accept-charset=\"utf-8\">";
		
	$formdata = createFormData($data[$i],"shortedit");
	//reformat this data to be 1 row with many columns
	$array = array();
	$headerarray = array();
	foreach($formdata as $k => $v){
		if($header){
			$headerarray[$v['name']] = "";
		}
		array_push($array,$v['data']);
	}
	if($header){
		$table->setHead($headerarray);
		$header = false;
	}	
	//$array[0] = $formstart .$array[0];
	//$array[count($array)-1] .= "</form>";
	
	$table->addContent($array);
	$out .= $formstart. $table->getTable() . "</form>";
	
	}
	
	

	return $out;
}
/**
 * Create form from parameters
 * @param unknown_type $target
 * @param unknown_type $data data array(title,type,name,value,other)
 *
 */
function createForm($target,$data){
	$out = "";
	$out .= "<form action='$target' method='POST' accept-charset=\"utf-8\">";
	$table = new HtmlTable();

	$formdata = createFormData($data);
	$table->setContent($formdata);
	$out .= $table->getTable();
	$out .= "</form>";

	return $out;
}
?>